package com.robusta.app.model.spreadsheet.loader;

import com.google.common.base.Function;
import com.robusta.spreadsheet.describe.TemplateInfo;
import com.robusta.spreadsheet.describe.WorkbookInfo;

import java.util.List;

import static com.google.common.collect.Lists.transform;

public class WorkbookTemplate implements com.robusta.spreadsheet.model.WorkbookTemplate {
    private WorkbookInfo workbookInfo;

    public WorkbookTemplate(WorkbookInfo workbookInfo) {
        this.workbookInfo = workbookInfo;
    }

    @Override
    public List<com.robusta.spreadsheet.model.WorksheetTemplate> getSpreadsheetTemplates() {
        return transform(workbookInfo.templatesInfo(), new Function<TemplateInfo, com.robusta.spreadsheet.model.WorksheetTemplate>() {
            @Override
            public com.robusta.spreadsheet.model.WorksheetTemplate apply(TemplateInfo input) {
                return new WorksheetTemplate(input);
            }
        });
    }

    public static class WorksheetTemplate implements com.robusta.spreadsheet.model.WorksheetTemplate {
        private TemplateInfo templateInfo;

        public WorksheetTemplate(TemplateInfo templateInfo) {
            this.templateInfo = templateInfo;
        }

        @Override
        public int getHeaderRowIndex() {
            return templateInfo.getHeaderRowIndex();
        }

        @Override
        public int getSheetIndex() {
            return templateInfo.getSheetIndex();
        }

        @Override
        public int getStartColumnIndex() {
            return templateInfo.getStartColumnIndex();
        }


        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || !com.robusta.spreadsheet.model.WorksheetTemplate.class.isAssignableFrom(o.getClass())) return false;

            com.robusta.spreadsheet.model.WorksheetTemplate that = (com.robusta.spreadsheet.model.WorksheetTemplate) o;

            if(this.getHeaderRowIndex() != that.getHeaderRowIndex()) {
                return false;
            } else if(this.getSheetIndex() != that.getSheetIndex()) {
                return false;
            } else if(this.getStartColumnIndex() != that.getStartColumnIndex()) {
                return false;
            }

            return true;
        }

        @Override
        public int hashCode() {
            return templateInfo != null ? templateInfo.hashCode() : 0;
        }
    }
}
